const fs = require('fs');
const querystring = require('querystring');

module.exports = function (req, res) {
  const url = req.url.split('/')[1];
  const method = req.method;
  const address = req.headers['x-real-ip'];
  const qs = querystring.parse(url.split('?')[1]);
  console.log(req.url, url, method, qs, address);
  res.setHeader('Content-Type', 'application/json;charset=utf-8');
  if (req.url == '/') {
    res.setHeader('Content-Type', 'text/html;charset=utf-8');
    res.end(fs.readFileSync('index.html'));
    return;
  }
  if (url == 'get' && method == 'GET') {
    res.end(JSON.stringify({
      code: '200',
      method,
      address
    }));
    return;
  }
  if (url == 'post' && method == 'POST') {
    res.end(JSON.stringify({
      code: '200',
      method,
      address
    }));
    return;
  }
  if (url == 'delete' && method == 'DELETE') {
    res.end(JSON.stringify({
      code: '200',
      method,
      address
    }));
    return;
  }
  if (url == 'put' && method == 'PUT') {
    res.end(JSON.stringify({
      code: '200',
      method,
      address
    }));
  }
  if (url == 'patch' && method == 'PATCH') {
    res.end(JSON.stringify({
      code: '200',
      method,
      address
    }));
    return;
  }
  if (url == 'basic-auth' && method == 'GET') {
    if (req.headers['authorization']) {
      let str = req.headers['authorization'].split('Basic ')[1];
      let [username, password] = Buffer.from(str, 'base64').toString().split(':');
      if (req.url.split('/')[2] == username && req.url.split('/')[3] == password) {
        res.statusCode = 200;
        res.end(JSON.stringify({
          code: '200',
          method,
          address,
          username,
          password,
          "authenticate": true
        }));
        return;
      }
    }
    res.setHeader('WWW-Authenticate', 'Basic realm="SecureArea"');
    res.statusCode = 401;
    res.end(JSON.stringify({
      code: '401',
      method,
      address,
      "authenticate": false
    }));
    return;
  }
  if (url == 'status') {
    let code = req.url.split('/')[2];
    if (!code) {
      res.setHeader('Content-Type', 'text/plain;charset=utf-8');
      res.end('未命中');
      return;
    }
    res.statusCode = code;
    res.end(JSON.stringify({
      code,
      method,
      address
    }));
    return;
  }
  if (url == 'base64' && method == 'GET') {
    let value = req.url.split('/')[2];
    if (!value) {
      data = 'jinqiu.wang';
      res.end(JSON.stringify({
        code: '200',
        method,
        address,
        data
      }));
      return;
    }
    data = Buffer.from(value, 'base64').toString('utf-8');
    res.end(JSON.stringify({
      code: '200',
      method,
      address,
      data
    }));
    return;
  }
  if (url == 'delay' && method == 'GET') {
    const delay = Number(req.url.split('/')[2]);
    if (delay > 10) {
      console.log(delay);
      res.end(JSON.stringify({
        code: '200',
        method,
        address,
        delay: 10,
      }));
      return;
    } else {
      setTimeout(() => {
        res.end(JSON.stringify({
          code: '200',
          method,
          address,
          delay,
        }));
      }, delay * 1000);
    }
    return;
  }
  if (req.url.indexOf('cookies/set') != -1 && method == 'GET') {
    const key = req.url.split('/')[3];
    const value = req.url.split('/')[4];
    if (key && value) {
      res.setHeader('Set-Cookie', [`${key}=${value};Path=/`]);
      res.end(JSON.stringify({
        code: '200',
        method,
        address,
        msg: '设置成功'
      }));
      return;
    } else {
      res.end(JSON.stringify({
        code: '200',
        method,
        address,
        msg: '设置失败了'
      }));
      return;
    }
  }
  if (req.url.indexOf('cookies/delete') != -1 && method == 'GET') {
    const key = req.url.split('/')[3];
    const cookies = req.headers.cookie;
    console.log(key, cookies);
    if(!(cookies || cookies?.indexOf(key) == -1)){
      res.end(JSON.stringify({
        code: '200',
        method,
        address,
        msg: '没有找到这个 cookie'
      }));
      return;
    }
    res.setHeader('Set-Cookie', [`${key}=123;Max-Age=-1;Path=/`]);
    res.end(JSON.stringify({
      code: '200',
      method,
      address,
      msg: '删除成功'
    }));
    return;
  }
  if (url == 'cookies' && method == 'GET') {
    const cookies = req.headers.cookie;
    res.end(JSON.stringify({
      code: '200',
      method,
      address,
      'cookies': cookies ? cookies : {}
    }));
    return;
  }
  res.setHeader('Content-Type', 'text/plain;charset=utf-8');
  res.end('未命中');
}